<?php
namespace Home\Model;
use Think\Model\ViewModel;
/**
 *   上架通知清单
 * @author dem
 *
 */
class  ShelvesViewModel extends ViewModel {
	public $viewFields=array(
		'ShelvesList'=>array('*'),
		'ShelvesDetailed'=>array('code'=>'U_ItemCode','name'=>'U_ItemName','_on'=>'ShelvesList.id=ShelvesDetailed.pid')
		);
	
	/**
	 * 添加清单主信息
	 */
	function addList($name){
	    $data['ctime']  = time();
	    $data['emp_no'] = session('emp_no');
	    $data['name']   = $name;
	    $mode = M('ShelvesList');
	    return $mode->add($data);
	    
	}
	
	/**
	 * pid code name
	 * @param unknown $data
	 */
	function addDetailed($detaileds=array()){
	    $mode = M('ShelvesDetailed');
	    foreach ($detaileds as $vo)
	    $mode->add($vo);
	    return true;
	    
	}
	
	/**
	 * 获取用户创建的清单列表
	 */
	static function getListById($id){
	    $mode = M('ShelvesList');
	    return  $mode->where("id='{$id}'")->find();
	     
	}
	
	
	/**
	 * 获取用户创建的清单列表
	 */
	static function getListByemp(){
	    $mode = M('ShelvesList');
	    $user = session('emp_no');
	    $map = [];
	    $map['emp_no|auth'] = array('like',"%{$user}%");
	    $list = $mode->where($map)->order('id desc')->select();
	    foreach ($list as $key=>$vo){
	        $type = '普通';
	        if($vo['type'] == 1)
            {
                $type = '活动';
            }else if ($vo['type'] == 2)
            {
                $type = '集采';
            }
	        $list[$key]['name']= $list[$key]['name'].'-'.$list[$key]['u_name'].'-('.$type.')';
	    }
	    return $list;
	    
	}
	
	/**
	 * 获取清单详情
	 * @param unknown $pid
	 * @return Ambigous <mixed, boolean, NULL, string, multitype:, unknown, object>
	 */
	static function getShelvesDetailed($pid){
	    $mode = M('ShelvesDetailed');
	    $res =  $mode->where("pid = {$pid} and status = 0")->select();
	    foreach ($res as $key=>$vo){
	        $res[$key]['ctime'] = substr( $res[$key]['ctime'], 0,11);
	    }
	    return $res;
	}
	
	
     /**
      * 添加清单详细
      */
	static function saveShelvesDetailed($data,$pid){
	    $mode = M('ShelvesDetailed');
	    $smode = M('ShelvesList');
        $ucodes= $smode->where("id={$pid}")->find();
        if ($ucodes['is_send'] == 1){
            return false;
        }
        else {
            foreach ($data as $vo) {
                unset($vo['id']);
                //$vo['u_codes'] = $ucodes['u_code'];
                $vo['oa_id'] = $vo['true_oa_id'];
                $vo['pid'] = $pid;
                $vo['ctime'] = date('Y-m-d H:i:s', time());
                $mode->add($vo);
            }
        }
	    return true;
	}

    /**
     * 修改清单详细
     */
    static function updateShelvesDetailed($data){
        $shelvesDetailedModel = M('ShelvesDetailed');
        $sapItemAttachmentModel = M('SapItemAttachment');
        //var_dump($data);die();
        if(isset($data['id'])&&$data['id']){
            //update
            $id= $data['id'];
            $data['pcode'] = trim($data['pcode']);
            unset($data['id']);

            //更新清单内数据
            echo  $shelvesDetailedModel->where("id={$id}")->save($data);

            //更新物料主数据'S附加信息
            $emp_no = session('emp_no');
            $data['emp_no'] = $emp_no;
            $data['updatetime'] = date('Y-m-d H:i:s');
            $check = $sapItemAttachmentModel->where("oa_id={$data['oa_id']}")->save($data);
            if (!$check)
            {
                $sapItemAttachmentModel->add($data);
            }

            //更新物料主数据'S UptodatePurchasePrice by r_purchase_price
            if (!empty($data['r_purchase_price'])) {
                $mainData = array('UptodatePurchasePrice' => $data['r_purchase_price']);
                echo M('SapItems')->where("item_id={$data['oa_id']}")->save($mainData);
            }
        }
        return true;

    }

    static function updateShelvesDetailedOnlyMallPrice($data){
        $shelvesDetailedModel = M('ShelvesDetailed');
        if(isset($data['id'])&&$data['id']){
            //update
            $id= $data['id'];
            unset($data['id']);

            //更新清单内数据
            echo  $shelvesDetailedModel->where("id={$id}")->save($data);

        }
        return true;

    }

    static function updateShelvesDetailedRemarkForXiaJia($data){
        $shelvesDetailedModel = M('ShelvesDetailed');
        if(isset($data['id'])&&$data['id']){
            //update
            $id= $data['inlist_id'];
            unset($data['inlist_id']);

            // shelves_list_id => pid(ShelvesDetailed)
            // inlist_id => id(ShelvesDetailed)
            //更新清单内数据
            echo  $shelvesDetailedModel->where("id={$id}")->save($data);

        }
        return true;

    }
	
	/**
	 * 删除清单详细
	 * @param unknown $ids
	 */
	static function delShelvesDetailed($ids){
	    $mode = M('ShelvesDetailed');
	    return $mode->where("id in ({$ids})")->delete();
	}
	
	
	/**
	 * 添加清单
	 */
	static function saveshelvesList($data){
	    $data = array('name'=>$data['name'],'emp_no'=>session('emp_no'),'ctime'=>date('Y-m-d H:i:s'),'u_code'=>$data['id'],'u_name'=>$data['text'],'is_sale'=>$data['is_cx']==''?0:1,'to_emp'=>$data['cm'],'type'=>$data['type']);
	    $mode = M('ShelvesList');
	    return  $mode->add($data);
	    
	}
	
	static function  getHtmlByListId($pid){
	   $data =  self::getShelvesDetailed($pid);
	   $content = '';
	   foreach ($data as $vo){
	  
	       $content .= "<tr><td>{$vo['code']}</td><td>{$vo['name']}</td><td>{$vo['gift_note']}</td></tr>";
	   }
	   
	   
	   $html = "<html> <head> <style type='text/css'> table{ line-height:25px;} th{ border:1px solid #aaa} td{ text-align:center;border:1px solid #aaa} tr:hover{ background-color:#FF9} </style> </head> <table cellpadding='0' cellspacing='0' > <tr style='background-color:#3498db;color: white;font-weight: bold;text-align: center;'><td style='width: 130px'>物料编号</td><td>物料名称</td><td style='width: 230px'>赠品</td></tr>{$content}</table> </html>";
	   
	   return $html;
	   
	   
	}
	
	/**
	 * 修改接收通知用户
	 */
	static function updateListToEmp($data){
	  
	   $emps = rtrim($data['listId'],',');
	   $mode = M('ShelvesList');
	   $tomails = explode(',', $data['cm']);
	   $tomails = array_unique($tomails);
	   $tomails = implode($tomails,',');
	   $ucode = $data['id'];
	   $uname = $data['text'];
	   return $mode->where("id={$data['listId']}")->save(array('to_emp'=>$tomails,'u_code'=>$ucode,'u_name'=>$uname));
	}
	
	/**
	 * 
	 * @param unknown $code 平台code
	 * @param unknown $itemCode 物料编号
	 */
	static function updateDetailed($code,$pid){
	    $mode = M('ShelvesDetailed');
	    $mode->where("code ='{$code}' and pid={$pid}")->save(array('status'=>1));
	    return $mode->where("code ='{$code}' and pid={$pid}")->find();
	  
	}
	
	static function updateDetailedAll($data){
	    $id = $data['id'];
	    unset($data['id']);
	    $mode = M('ShelvesDetailed');
	    return $mode->where("id={$id}")->save($data);
	    
	}
	
	/**
	 * 根据用户 修改用户订单授权
	 * @param unknown $users
	 */
	static function updateAuthByUser($users){
	    $emp  = session('emp_no');
	    if(!$emp) return false;
	    $mode = M('ShelvesList');
	
	    $mode->where("emp_no='{$emp}'")->save(array('auth'=>$users));
	    $mode = M('ShelvesAuth');
	    $mode->where("emp_no='{$emp}'")->delete();
	    return $mode->data(array('emp_no'=>$emp,'auth'=>$users))->add();
	}
	
	static function updateIs_send($listId,$userlist,$listRemark){
	   $detailModel = M('ShelvesDetailed');
	   $now = date('Y-m-d H:i:s');
	   $detailModel->where("pid={$listId}")->save(array('status'=>1,'ctime'=>$now));

	   $smode = M('ShelvesList');
	   return $smode->where("id={$listId}")->save(array('is_send'=>1,'to_emp'=>$userlist,'ctime'=>$now,'remark'=>$listRemark));
	}

	
}
?>